package com.longcai.controller.order;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.longcai.bean.ApiResult;
import com.longcai.bean.PageHelper;
import com.longcai.entity.customer.ClyjUser;
import com.longcai.entity.nearby.ClyjNearbyShops;
import com.longcai.entity.order.ClyjGoodsOrderDetails;
import com.longcai.entity.order.ClyjGoodsOrderInfo;
import com.longcai.feign.client.OrderFeignClient;
import com.longcai.page.table.PageTableResponse;
import com.longcai.service.ClyjNearbyShopsService;
import com.longcai.service.clyjuser.ClyjUserService;
import com.longcai.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Administrator
 * @Title: ClyjGoodsOrderController
 * @ProjectName clyj-electricity
 * @Description: TODO
 * @date 2019/4/2915:26
 */
@RestController
@RequestMapping(value = "/goodsOrder/")
public class ClyjGoodsOrderController {

    @Autowired
    private OrderFeignClient orderFeignClient;
    @Autowired
    private ClyjNearbyShopsService clyjNearbyShopsService;
    @Autowired
    private ClyjUserService clyjUserService;

    /**
     * 查询订单列表
     */
    @GetMapping(value = "selectByShopId")
    @ResponseBody
    public PageTableResponse selectOrderByShopsId(ClyjGoodsOrderDetails clyjGoodsOrderDetails,PageHelper pageHelper) {

        String userId = UserUtil.getLoginUser().getUserId();
        ///getUserIdString userId = "clyj_uAcuMY55";
        QueryWrapper<ClyjNearbyShops> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mich_id", "clyj_uAcuMY55");
        ClyjNearbyShops cns = clyjNearbyShopsService.selectOne(queryWrapper);
        String shopsId = cns.getShopsId();
        JSONObject object = new JSONObject();
        object.put("shopsId", shopsId);
        object.put("orderStatus",clyjGoodsOrderDetails.getOrderStatus());
        object.put("ordetType",clyjGoodsOrderDetails.getOrderType());

        Map<String,Object> map = new HashMap<>();
        map.put("params",object);
        map.put("pageHelper",pageHelper);
        return this.orderFeignClient.selectOrderList(map);
    }

    /**
     * 查询订单总数量
     */
    @RequestMapping(value = "selectByShopId2")
    @ResponseBody
    public ApiResult selectByShopId2( ClyjGoodsOrderDetails clyjGoodsOrderDetails) {
        String userId = UserUtil.getLoginUser().getUserId();
        ///getUserIdString userId = "clyj_uAcuMY55";
        QueryWrapper<ClyjNearbyShops> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mich_id", userId);
        ClyjNearbyShops cns = clyjNearbyShopsService.selectOne(queryWrapper);
        String shopsId = cns.getShopsId();
        JSONObject object = new JSONObject();
        object.put("shopsId", shopsId);
        object.put("orderStatus",clyjGoodsOrderDetails.getOrderStatus());
        object.put("ordetType",clyjGoodsOrderDetails.getOrderType());

        PageHelper pageHelper = new PageHelper();
        pageHelper.setStart(Integer.valueOf(0));
        pageHelper.setLength(Integer.valueOf(10));
        Map<String,Object> map = new HashMap<>();
        map.put("params",object);
        map.put("pageHelper",pageHelper);
        PageTableResponse a = this.orderFeignClient.selectOrderList(map);
        return ApiResult.success(a.getRecordsTotal());
    }

    /**
     * 根据订单号查询订单详情
     *
     * @param
     * @return
     */
    @GetMapping(value = "selectOrderBytradeNo")
    @ResponseBody
    public ApiResult selectOrderBytradeNo(HttpServletRequest request) {
        String tradeNo = request.getParameter("tradeNo");
        ApiResult apiResult = this.orderFeignClient.selectOrderByTradeNo(tradeNo);
        ClyjGoodsOrderDetails clyjGoodsOrderDetails = null;
        ClyjUser clyjUser = null;
        if(apiResult.getData() != null){
            clyjGoodsOrderDetails = JSON.parseObject(JSON.toJSONString(apiResult.getData()),ClyjGoodsOrderDetails.class);
            String userId = clyjGoodsOrderDetails.getUserId();
            ApiResult apiResult2 = this.clyjUserService.getById(userId);
            if(apiResult2.getData() != null){
                clyjUser = JSON.parseObject(JSON.toJSONString(apiResult2.getData()),ClyjUser.class);
            }
        }
        Map<String,Object> map = new HashMap<>();
        map.put("clyjGoodsOrderDetails",clyjGoodsOrderDetails);
        map.put("clyjUser",clyjUser);
        apiResult.setData(map);
        return apiResult;
    }

    @GetMapping(value = "updateOrderInfo")
    @ResponseBody
    public ApiResult updateOrderInfo(HttpServletRequest request) {
        String tradeNo = request.getParameter("tradeNo");
        String orderStatus = request.getParameter("orderStatus");
        Map<String,Object> map = new HashMap<>();
        map.put("tradeNo",tradeNo);
        map.put("orderStatus",orderStatus);
        ApiResult apiResult = this.orderFeignClient.updateOrderInfo(map);
        return apiResult;
    }

}